package gccshop.dao.impl;

import gccshop.dao.DbConn;
import gccshop.dao.SellerDao;
import gccshop.pojo.Seller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

//持久层接口实现类，同時继承数据库连接类，
/**
 * @author Administrator
 * 
 */
public class SellerDaoImpl extends DbConn implements SellerDao {
	// 定义要操作的表
	String tableName = "t_seller";
	// 封装要查询的全字段
	String[] findKeys = { "Id", "shopName", "shopLocation", "golden" };
	//
	private ResultSet rs = null;
	private Seller entity = null;
	private ArrayList<Seller> entityList = null;

	@Override
	public int add(Seller entity) {
		int f = 0;
		String sql = "insert into t_seller(shopName,shopLocation,golden) values(?,?,?)";
		Object[] objects = { entity.getShopName(), entity.getShopLocation(),
				entity.getGolden() };
		f = update(sql, objects);

		try {
			closeConn();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return f;
	}

	// 批量删除
	// delete from t_seller where Id in (1965,1966);

	@Override
	public int delete(String[] id) {

		int f = 0;
		String sql = "delete from t_seller where Id in (";
		sql += id[0];
		if (id.length > 1) {
			for (int i = 1; i < id.length; i++) {
				sql += "," + id[i];
			}
		}
		sql += ")";
		f = update(sql, null);
		try {
			closeConn();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return f;
	}

	@Override
	public int modify(Seller e) {
		int f = 0;
		String sql = "update t_seller set shopName=?,shopLocation=?,golden=?  where Id=?";
		Object[] objects = { e.getShopName(), e.getShopLocation(),
				e.getGolden(), e.getId() };
		f = update(sql, objects);
		try {
			closeConn();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		return f;
	}

	// 精確查找
	@Override
	public Seller findEntity(String p, Object o) {
		// 一定要记得新定义一个entity，防备上次查找的结果对本次查找造成干扰
		Seller entity = null;
		String sql = "Select * from t_seller where " + p + "=?";
		Object[] objects = { o };
		try {
			rs = query(sql, objects);
			if (rs.next()) {
				// 一旦找到，则完整包装此对象
				entity = new Seller();
				entity.setId(rs.getInt("Id"));
				entity.setGolden(rs.getString("golden"));
				entity.setShopLocation(rs.getString("shopLocation"));
				entity.setShopName(rs.getString("shopName"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				closeConn();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return entity;
	}

	@Override
	// 全字段查询符合条件的记录数
	// findValue：要查询的关键字
	public int findTotal(String findValue) {
		return super.findTotal(tableName, findKeys, findValue);
	}

	// select * from t_seller where shopName like '%测试%' limit 0,10;
	// 按条件分页查找
	/*
	 * tableName:要查询的表名
	 * 
	 * findKey:要查询的关键字
	 * 
	 * findValue:要模糊查询的匹配词
	 * 
	 * pageNo:第几页
	 * 
	 * resultsperpage:每页要显示的记录数
	 */
	@Override
	public ArrayList<Seller> findEntities(String findValue, int pageNo,
			int rowsPerPage) {
		try {
			// 分页查询
			// 第一个？代表起始记录号：为（页编号-1）*每页要显示的记录数
			// 第二个？代表每页要显示的记录数
			String sql = " select * from " + tableName;
			sql += " where " + findKeys[0] + " like '%" + findValue + "%' ";
			if (findKeys.length > 1) {
				for (int i = 1; i < findKeys.length; i++) {
					sql += " or " + findKeys[i] + " like '%" + findValue
							+ "%' ";
				}
			}
			sql += " limit ?,?";
			// 植入参数
			Object[] objects = { (pageNo - 1) * rowsPerPage, rowsPerPage };
			// 执行查询
			rs = query(sql, objects);
			// 构造对象列表
			entityList = new ArrayList<Seller>();
			// 循环解析记录集中的数据，将其转换为对象，并添加到对象列表
			while (rs.next()) {
				entity = new Seller();// 生成对象
				// 填充对象
				entity.setId(rs.getInt("Id"));
				entity.setGolden(rs.getString("golden"));
				entity.setShopLocation(rs.getString("shopLocation"));
				entity.setShopName(rs.getString("shopName"));
				entityList.add(entity);// 将对象添加到列表
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				closeConn();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return entityList;
	}
}
